package v4

import (
	"fmt"
	"log"
	"mindonmap/pkgs/e"
	"mindonmap/pkgs/setting"
	"mindonmap/pkgs/util"
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"mindonmap/validation"

	"mindonmap/models"
)

// 获取指定历史数据
func GetMindVersion(c *gin.Context) {
	var json models.MindVersion
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}

	valid := validation.Validation{}
	valid.Required(json.ID, "pid").Message("不能为空")

	code := e.INVALID_PARAMS
	var data interface{}
	if !valid.HasErrors() {
		if models.ExistMindVersionByID(json.ID, json.Version) {
			data = models.GetMindVersion(json.ID, json.Version)
			code = e.SUCCESS
		} else {
			code = e.ERROR_NOT_EXIST_ARTICLE
		}
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}

// 获取脑图历史列表
func GetMindVersions(c *gin.Context) {
	data := make(map[string]interface{})
	type mindVersionmap struct {
		Pid     string `json:"pid"`
		Version int    `json:"version"`
	}

	var maps mindVersionmap
	valid := validation.Validation{}
	if err := c.BindJSON(&maps); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}

	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		code = e.SUCCESS
		Pagesize := 4
		if IsVip(c) {
			Pagesize = 50
		}
		fmt.Println(util.GetPage(c), Pagesize, maps.Pid)
		if maps.Version == 1 {
			data["lists"] = models.GetMindVersionsAll(util.GetPage(c), Pagesize, maps.Pid)
		} else {
			data["lists"] = models.GetMindVersions(util.GetPage(c), Pagesize-1, maps.Pid)
		}

		//data["total"] = models.GetMindVersionTotal(maps)
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}

// 新增脑图历史数据
func AddMindVersion(c *gin.Context) {
	var json models.MindVersion

	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}
	Userid, err := GetUserID(c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT,
			"msg":  e.GetMsg(e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT),
			"data": make(map[string]interface{}),
		})
		return
	}
	timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
	image := util.WriteFile(strconv.Itoa(Userid), json.Pid+timestamp, json.Image)
	if models.ExistMindMapByID(json.Pid, Userid) {
		if len(image) > 0 {
			var aws3 = setting.GetAWSS3()
			S3Host := aws3.S3Host
			image = S3Host + image
		}
		data := make(map[string]interface{})
		data["acid"] = Userid
		data["image"] = image
		if len(json.Image) > 0 {
			models.EditMindMap(json.Pid, data)
		}
	}

	code := e.SUCCESS
	data := make(map[string]interface{})

	data["pid"] = json.Pid
	data["acid"] = Userid
	data["mapdata"] = json.Mapdata
	data["state"] = 0
	data["image"] = image
	data["version"] = 2
	models.AddMindVersion(data)

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": data,
	})
}

// 删除脑图
func DeleteMindVersion(c *gin.Context) {
	var json models.MindVersion
	if err := c.BindJSON(&json); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": e.INVALID_PARAMS,
			"msg":  e.GetMsg(e.INVALID_PARAMS),
			"data": make(map[string]interface{}),
		})
		return
	}

	valid := validation.Validation{}
	valid.Required(json.Version, "version").Message("不能为空")
	valid.Required(json.ID, "id").Message("不能为空")

	code := e.INVALID_PARAMS
	if !valid.HasErrors() {
		if models.GetMindVersionID(json.ID, json.Version) {
			models.DeleteMindVersion(json.ID, json.Version)
			code = e.SUCCESS
		} else {
			code = e.ERROR_NOT_EXIST_ARTICLE
		}
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.key: %s, err.message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": make(map[string]string),
	})
}
